<?
/**
* Copyright (c) 2002-2007 BioWare Corp.
* http://www.bigdaddymonitor.org
*
* This file is part of the BigDaddy Monitoring
* program. It is free software distributed under
* the terms of the BSD License. It is ditributed
* in the hopes that it will be useful, but
* WITHOUT ANY WARRANTY; without event the implied
* warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE.
*
* The BSD License is available from:
* http://www.opensource.org/licenses/bsd-license.php
**/
/*===========================================================================================*/
//BigDaddy CORE Functions
//This provides core functions for BigDaddy
//DW 03.12.04
/*===========================================================================================*/

//Returns the root path for the treeview
//DW 02.09.04
function rootEntryPathCreate($ID)
{

	$rootParent=$ID;

	while ($rootParent>0)
	{
		$sql="SELECT name, parentID FROM treeEntry WHERE ID='".$rootParent."'";
		$parentInfo=mysql_fetch_array(run_mysql_query($sql));

		$rootPath=$parentInfo["name"]." / ".$rootPath;
		$rootParent=$parentInfo["parentID"];
	}

	return $rootPath;

}

/*---------------------------------------------------------------------------------
 string ConvertUnixToMySQLDateTimeString (int nStamp)
 Created by Duleepa Wijayawardhana
 January 12, 2004
 
 Description: Converts Unix Timestamp to a MYSQL compatible DateTime String
 'YYYY-MM-DD HH:MM:SS' 
 ---------------------------------------------------------------------------------*/
function ConvertUnixToMySQLDateTimeString($nStamp)
{
	return strftime("%Y-%m-%d %T", $nStamp);
}

/*===========================================================================================*/
//str get_time_str ([int UNIX_TIMESTAMP, [int Type]])
//Formats and displays time/date for a given timestamp (or defaults to current time 
//if not specified
//Created DW / Modified: DW 15.03.2004
/*===========================================================================================*/
function get_time_str($stamp="current_time", $type=6) 
{
	if ($stamp=="current_time")
	{
		$stamp=time();
	}
	
	if ($stamp>10) 
	{

		switch ($type)
		{
			case 1:
				//Tuesday, 4 March 2002 08:33PM
				return (strftime("%A, %d %B %Y %T", $stamp));
			case 2:
				//08:33 2002-03-04 
				return (strftime("%T %D", $stamp));
			case 3:
				//4 Mar 2002
				return (strftime("%d %b %Y", $stamp));
			case 4:
				//Tue, 4 March 2002 08:33PM
				return (strftime("%a, %d %B %I:%M%p", $stamp));
			case 5:
				//Mar 4, 08:33PM
				return (strftime("%b %d, %I:%M%p", $stamp));
			case 6:
				//Tuesday, 4 March 2002
				return (strftime("%A, %d %B %Y", $stamp));
			case 7:	
				//March 02
				return (strftime("%B %Y", $stamp));
			case 8:	
				//Tuesday, March 4, 2002
				return (strftime("%A, %B %e, %Y", $stamp));
			case 9:
				//Tuesday, March 4, 2002 -- 08:33PM
				return (strftime("%A, %B %e, %Y -- %I:%M:%p", $stamp));
			case 10:
				//March 4
				return (strftime("%B %e", $stamp));
			case 11:
				//Tuesday
				return (strftime("%A", $stamp));
			case 12:
				//Hour (00-23)
				return (strftime("%H", $stamp));
			case 13:
				//March 4, 2002
				return (strftime("%B %e, %Y", $stamp));
			case 14:	
				//4 Mar 2002
				return (strftime("%A %d %b %Y", $stamp));
			case 15:
				//01:23:45 (hours:minutes:seconds)
				return (strftime("%T", $stamp));
			case 16:
				//01/02 (month.day)
				return (strftime("%m/%d", $stamp));
			case "timeline":
				//For the simile timeline
				return (strftime("%B %d %Y %T", $stamp)." GMT");
		}
		
	}
}

/*===========================================================================================*/
//void create_dateselect(str dayselect, str monthselect, str yearselect [int hourminswitch [str hourselect [str minuteselect]]])
//Creates dateselect HTML and sets the select names to the strings supplied
//DW
/*===========================================================================================*/

function create_dateselect($dayname, $monthname, $yearname, $time=99, $granularity=NULL, $hourname=NULL, $minutename=NULL)
{

//Global Months Array
$months=array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");


$aCurrentDate = getdate();
$nCurrentYear = $aCurrentDate["year"];


if ($time=="99")
{
	$time=time();
}
?>

<select name="<?=$dayname;?>" size=1><?

$old=getdate($time);

for($i=1;$i<=31;$i++) 
{
	if ($i==$old["mday"]) 
	{
		$s=" selected";
	} 
	else 
	{
		$s="";
	}
	echo "<OPTION value=".$i."".$s.">".$i."</option>";
}
?></select>
			
<select name="<?=$monthname;?>" size=1><?

for($i=1;$i<=12;$i++) 
{
					
	if ($i==$old["mon"]) 
	{
		$s=" selected";
	} 
	else 
	{
		$s="";
	}
		echo "<OPTION value=".$i."".$s.">".$months[($i-1)]."</option>";
}
?> </select>
				
<select name="<?=$yearname;?>" size=1><?


for($i=2000;$i<=($nCurrentYear+1);$i++) 
{
	if ($i==$old["year"]) 
	{
		$s=" selected";
	} 
	else
	{
		$s="";
	}
	echo "<OPTION value=".$i."".$s.">".$i."</option>";
}
?> </select>

<?
if ($granularity==1)
{

?>
// 
<select name="<?=$hourname;?>" size=1><?
	for($i=0;$i<=24;$i++) 
	{
		if ($i==$old["hours"]) 
		{
			$s=" selected";
		}
		else
		{
			$s="";
		}
		
		echo "<OPTION value=".$i."".$s.">".addzero_less_ten($i)."</option>";
	}
?> </select>
:
<select name="<?=$minutename;?>" size=1><?
	for($i=0;$i<=59;$i++) 
	{
		if ($i==$old["minutes"]) 
		{
			$s=" selected";
		}
		else
		{
			$s="";
		}
		echo "<OPTION value=".$i."".$s.">".addzero_less_ten($i)."</option>";
	}
?> </select>
<?

}
?>

<?
}

/*===========================================================================================*/
//str addzero_less_ten(int number)
//Adds a zero to display number if less than 10
//DW
/*===========================================================================================*/

function addzero_less_ten($num) 
{
	if ($num < 10) 
	{
		$num = "0".$num;
	}
	return $num;
}

/*===========================================================================================*/
//void showMonitoringNav()
//Displays the sub navigation for the Monitoring Options Section
//DW 18.03.2004
/*===========================================================================================*/

function showMonitoringNav()
{
?>
<br />
<table border="0" cellpadding="0" cellspacing="0" width="98%">
<tr>
<td width="100%">

</td>
<td width="400" nowrap>
<div class="orgbar"><span class="text">
Monitoring: 
	<a href="<?=$_SERVER["PHP_SELF"];?>?page=MonIniFiles">IniFiles</a> |
	<a href="<?=$_SERVER["PHP_SELF"];?>?page=MonModules">Modules</a>
</span>
</div>
</td>
</tr>
</table>

<?
}

/*===========================================================================================*/
//void showServerNav()
//Displays the sub navigation for the Server Control Section
//DW 18.03.2004
/*===========================================================================================*/

function showServerNav()
{
?>
<br />
<table border="0" cellpadding="0" cellspacing="0" width="98%">
<tr>
<td width="100%">

</td>
<td width="400" nowrap>
<div class="orgbar"><span class="text">
Servers: 
	<a href="<?=$_SERVER["PHP_SELF"];?>?page=ServerIndex">Server Listing</a> |
	<a href="<?=$_SERVER["PHP_SELF"];?>?page=ServerCategory">Server Categories</a> 
</span>
</div>
</td>
</tr>
</table>

<?
}

/**
 * Converts seconds to days/hours and minutes
 */
function convertTimeToHours($nSeconds)
{
	$nMinutes = floor($nSeconds/60);

	$nHours = floor($nMinutes/60);
	$nReportMins = $nMinutes - ($nHours*60);
	
	$nDays = floor($nHours/24);
	$nReportHours = $nHours - ($nDays * 24);
	
	
	return "$nDays d. $nReportHours h. $nReportMins m.";
}
/*===========================================================================================*/
//void cronstart(int cronID)
//Joins the cron to the crontab control
//DW 19.03.2004
/*===========================================================================================*/

function cronstart($cronID, $sOptions=NULL)
{
	//Use curl to go fetch the URL and then die if fail, otherwise all good
	$url = BIGDADDY_API_URL."?page=SetCronStart&cronID=".$cronID."&option=".$sOptions;
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$contents = curl_exec($ch);
	curl_close($ch);
	
	if (substr_count(strtolower($contents), "fail") >0)
	{
		echo "Cron Cannot Be Started";
		exit;
	}
}

/*===========================================================================================*/
//void cronend(int cronID)
//Sends the end result to the crontab control.
//DW 19.03.2004
/*===========================================================================================*/

function cronend($cronID)
{
	$url = BIGDADDY_API_URL."?page=SetCronEnd&cronID=".$cronID;
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$contents = curl_exec($ch);
	curl_close($ch);
	
}

/*===========================================================================================*/
//void SetCronLogEntry(int cronID, string sText)
//Sets a CronLogEntry
//DW 28.04.2005
/*===========================================================================================*/
function SetCronLogEntry($cronID, $sText)
{
    $url = BIGDADDY_API_URL."?page=SetCronEnd&cronID=".$cronID."&log=".urlencode($sText);
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$contents = curl_exec($ch);
	curl_close($ch);
}

?>
